﻿using HotelManager.Common;
using HotelManager.Extensions;
using HotelManager.Models;
using HotelManager.Service;
using Prism.Commands;
using Prism.Ioc;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls.Primitives;

namespace HotelManager.ViewModels
{
    public class TypeViewModel: NavigationViewModel
    {
        private readonly IDialogHostService dialog;
        private readonly IRegionManager regionManager;
        private readonly ITypeService typeService;
        public TypeViewModel(IDialogHostService dialog, IContainerProvider provider, ITypeService _typeService) : base(provider)
        {
            regionManager = provider.Resolve<IRegionManager>();
            this.dialog = dialog;
            typeService= _typeService;
            RoomTypeLists = new ObservableCollection<RoomTypeData>();
            ExecuteCommand = new DelegateCommand<string>(Execute);
            SelectedCommand = new DelegateCommand<RoomTypeData>(Selected);
            DelCommand = new DelegateCommand<RoomTypeData>(Delete);
        }
        private async void Delete(RoomTypeData obj)
        {
            var dialogResult = await dialog.Question("温馨提示", $"确认删除房型：{obj.RoomTypeName}?");
            if (dialogResult.Result != ButtonResult.OK) return;
            var deleteResult = await typeService.DeleteAsync(obj.RoomTypeId);
            if (deleteResult.success)
            {
                var model = RoomTypeLists.FirstOrDefault(o => o.RoomTypeId.Equals(obj.RoomTypeId));
                if (model != null)
                    RoomTypeLists.Remove(model);
            }
        }
        private void Execute(string Operater)
        {
            switch (Operater)
            {
                case "Add":
                    OpenDraw();
                    break;
                case "Save":
                    Save();
                    break;
            }
        }
        private void OpenDraw()
        {
            CurrentDto = new RoomTypeData();
            IsRightDrawerOpen = true;
        }
        private async void Selected(RoomTypeData roomTypeData)
        {
            try
            {
                UpdateLoading(true);
                var memoResult = await typeService.GetFirstOrDefaultAsync(roomTypeData.RoomTypeId);
                if (memoResult is not null)
                {
                    CurrentDto = memoResult;
                    IsRightDrawerOpen = true;
                }
            }
            catch (Exception ex)
            {

            }
            finally
            {
                UpdateLoading(false);
            }
        }
        private async void Save()
        {
            if (string.IsNullOrWhiteSpace(CurrentDto.RoomTypeName))
                return;
            UpdateLoading(true);
            try
            {
                if (CurrentDto.RoomTypeId > 0)
                {
                    var updateResult = typeService.Update(CurrentDto);
                    if (updateResult.success)
                    {
                        var todo = RoomTypeLists.FirstOrDefault(t => t.RoomTypeId == CurrentDto.RoomTypeId);
                        if (todo != null)
                        {
                            todo.RoomTypeName = CurrentDto.RoomTypeName;
                            GetDataAsync();
                        }
                    }
                    IsRightDrawerOpen = false;
                }
                else
                {
                    var addResult = await typeService.AddAsync(CurrentDto);
                    if (addResult.success)
                    {
                        GetDataAsync();
                        IsRightDrawerOpen = false;
                    }
                }
            }
            catch (Exception ex)
            {

            }
            finally { UpdateLoading(false); }
        }
        private bool isRightDrawerOpen;

        public bool IsRightDrawerOpen
        {
            get { return isRightDrawerOpen; }
            set { isRightDrawerOpen = value; RaisePropertyChanged(); }
        }
        private RoomTypeData currentDto;

        public RoomTypeData CurrentDto
        {
            get { return currentDto; }
            set { currentDto = value; RaisePropertyChanged(); }
        }
        public ObservableCollection<RoomTypeData> roomTypeLists;
        public ObservableCollection<RoomTypeData> RoomTypeLists
        {
            get { return roomTypeLists; }
            set { roomTypeLists = value; RaisePropertyChanged(); }
        }
        public DelegateCommand<string> ExecuteCommand { get; private set; }
        public DelegateCommand<RoomTypeData> SelectedCommand { get; private set; }
        public DelegateCommand<RoomTypeData> DelCommand { get; private set; }
        async void GetDataAsync()
        {
            UpdateLoading(true);

            var memoResult = await typeService.GetAllAsync();
            if (memoResult.Any())
            {
                RoomTypeLists.Clear();
                foreach (var item in memoResult)
                {
                    RoomTypeLists.Add(item);
                }
            }
            UpdateLoading(false);
        }
        public override void OnNavigatedTo(NavigationContext navigationContext)
        {
            base.OnNavigatedTo(navigationContext);
            GetDataAsync();
        }
    }
}
